package com.quad.innovators.salesease.config;



import com.quad.innovators.salesease.constants.SystemConstants;
import com.quad.innovators.salesease.interceptor.LoginInterceptor;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 拦截器
 * @author mujingjing
 * @date 2023/2/14
 **/
@Configuration
@Getter
@Setter
public class LoginInterceptorConfig implements WebMvcConfigurer {

    /**
     * 登录拦截器
     */
    private final LoginInterceptor loginInterceptor;

    @Value("${open-swagger:false}")
    private Boolean openSwagger;

    public LoginInterceptorConfig(LoginInterceptor loginInterceptor) {
        this.loginInterceptor = loginInterceptor;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 配置拦截器访问静态资源
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/favicon.ico").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        String[] swaggerPathPatterns = new String[]{
                "/doc.html", "/webjars/**", "/v3/api-docs/**", "/swagger-ui.html"
        };

        String[] excludePathPatterns = new String[]{
                SystemConstants.BACKEND_API + "/auth/login",
        };
        registry.addInterceptor(loginInterceptor).addPathPatterns("/**")
                .excludePathPatterns(excludePathPatterns).excludePathPatterns(swaggerPathPatterns);
    }
}
